<?php

namespace app\admin\controller\depot;

use app\common\controller\Backend;


/**
 * vue商城
 *供应商
 * @icon fa fa-user
 */
class Supplier extends Backend
{
    protected $relationSearch = true;
    protected $RegionModel = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Depot.Supplier');
        // 城市模型
        $this->RegionModel = model('Region.Region');
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with('region')
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('region')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if (!empty($params)) {
                $this->error("参数不能为空");
            }
            $res['code'] = 1;
            $add = explode('/', $params['city']);
            $index = count($add);
            if ($index != 3) {
                $this->error('地址选到区域');
            }
            $parentpath = $this->RegionModel->where('name', $add[$index - 1])->value('parentpath');
            $parentpath = explode(',', $parentpath);
            if (!$parentpath) {
                $this->error('城市不存在');
            }
            $data = [
                'name' => $params['name'],
                'mobile' => $params['mobile'],
                'province' => $parentpath[0] ?? '',
                'city' => $parentpath[1] ?? '',
                'district' => $parentpath[2] ?? '',
                'address' => $params['address'] ?? ''
            ];
            $result = $this->model->validate('common/Depot/Supplier')->save($data);
            if (!$result) {
                $this->error($this->model->getError());
            }
            $this->success();
        }
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->find($ids);
        if (!$row) {
            $this->error(__('当前供货商不存在'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if (empty($params)) {
                $this->error("参数不能为空");
            }
            $add = explode('/', $params['city']);
            $index = count($add);
            if ($index != 3) {
                $this->error('地址选到区域');
            }
            $parentpath = $this->RegionModel->where('name', $add[$index - 1])->value('parentpath');
            $parentpath = explode(',', $parentpath);
            if (!$parentpath) {
                $this->error('城市不存在');
            }
            $data = [
                'id' => $ids,
                'name' => $params['name'],
                'mobile' => $params['mobile'],
                'province' => $parentpath[0] ?? '',
                'city' => $parentpath[1] ?? '',
                'district' => $parentpath[2] ?? '',
                'address' => $params['address'] ?? ''
            ];
            $result = $this->model->validate('common/Depot/Supplier')->isUpdate()->save($data);
            if ($result === false) {
                $this->error($this->model->getError());
            }
            $this->success();
        }

        $this->assign('row', $row);
        return $this->view->fetch();
    }
    /**
     *删除
     */
    public function del($ids = null)
    {
        $List = $this->model->all($ids);
        if (empty($List)) {
            $this->error('当前供货商不存在');
        }
        $result = $this->model->destroy($ids);
        if (!$result) {
            $this->error($this->model->getError());
        }

        $this->success();
    }
}
